package com.igeek.ch02.service;

import com.igeek.ch02.dao.ItemsDao;
import com.igeek.ch02.entity.Items;
import com.igeek.ch02.vo.PageVO;

import java.sql.SQLException;
import java.util.List;

/**
 * @author chenmin
 * 2023/7/27 10:17
 * @description TODO
 */
public class ItemsService {

    private ItemsDao dao = new ItemsDao();

    //商品条件分页查询
    public PageVO<Items> fenye(int current , String query){
        PageVO<Items> vo = new PageVO<>();
        vo.setCurrent(current);
        vo.setQuery(query);
        try {
            //查询总记录数
            int myCounts = dao.selectCounts(query);
            vo.setMyCounts(myCounts);
            //计算总页数
            int myPages = myCounts%3==0?myCounts/3:myCounts/3+1;
            vo.setMyPages(myPages);

            //计算begin
            int begin = (current-1)*3;
            //查询列表
            List<Items> items = dao.selectAllItems(query, begin);
            vo.setList(items);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return vo;
    }

    //通过ID编号查询商品信息
    public Items findOne(int id){
        Items items = null;
        try {
            items = dao.selectOneById(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return items;
    }

    //更新
    public boolean updateItemsById(Items items){
        int i = 0;
        try {
            i = dao.updateItems(items);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i>0?true:false;
    }
    public boolean addItem(Items items) {
        int i=0;
        try {
            i = dao.addItems(items);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i > 0 ? true:false;
    }
}
